package com.cmh.mynfc;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * Description:
 * 子类在onNewIntent方法中进行NFC标签相关操作。
 * launchMode设置为singleTop或singelTask，保证Activity的重用唯一
 * 在onNewIntent方法中执行intent传递过来的Tag数据
 * 将NFC标签卡靠近手机后部
 */
public abstract class BaseNfcActivity extends Activity {
    protected static final String TAG = "BaseNfcActivity";
    protected NfcAdapter mNfcAdapter;
    private PendingIntent mPendingIntent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getIntent();
    }

    /**
     * onCreat->onStart->onResume->onPause->onStop->onDestroy
     * 启动Activity，界面可见时.
     */
    @Override
    protected void onStart() {
        super.onStart();
        //此处adapter需要重新获取，否则无法获取message

        Intent intent = getIntent();
        Log.e(TAG, "获得Intent: action = " + intent.getAction());
        if(intent.hasExtra(Intent.EXTRA_TEXT)){
            String data = intent.getStringExtra(Intent.EXTRA_TEXT);
            Log.e(TAG, "获得Intent1: data = " + data);
        }

        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if(mNfcAdapter == null){
            //to to sth..
            return;
        }else{
            if(!mNfcAdapter.isEnabled()){
                Intent nfcSet = new Intent(Settings.ACTION_NFC_SETTINGS);
                startActivity(nfcSet);
            }
        }
        //一旦截获NFC消息，就会通过PendingIntent调用窗口
        Log.e(TAG, "截获NFC消息");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), PendingIntent.FLAG_MUTABLE);
        } else {
            mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), PendingIntent.FLAG_ONE_SHOT);
        }

        if(mPendingIntent!=null){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                Log.e(TAG, "package = " + mPendingIntent.getCreatorPackage());
            }
        }


    }

    /**
     * 获得焦点，按钮可以点击
     */
    @Override
    public void onResume() {
        super.onResume();
        //设置处理优于所有其他NFC的处理
        if (mNfcAdapter != null) {
            mNfcAdapter.enableForegroundDispatch(this, mPendingIntent, null, null);
            enableReaderMode();
        }
    }

    private void enableReaderMode() {
//        int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
        int READER_FLAGS = NfcAdapter.FLAG_READER_NFC_A;
        //	| NfcAdapter.FLAG_READER_NO_PLATFORM_SOUNDS;
        Bundle options = new Bundle();
        options.putInt(NfcAdapter.EXTRA_READER_PRESENCE_CHECK_DELAY, 50);// 延迟对卡片的检测
        if (mNfcAdapter != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mNfcAdapter.enableReaderMode(BaseNfcActivity.this, tag -> {
                    //这里不是主线程,不能直接让textview设置内容，
                    //api19 不从onNewIntent走，而是直接跳到这个回调函数里
    //                tagShowMethodAPI19(tag);
                    Log.e(TAG, "enableReaderMode: ");
                    onTagDiscovered(tag);
                }, READER_FLAGS, null);
            }
        }
    }

    public abstract void onTagDiscovered(Tag detectedTag);

    /**
     * 暂停Activity，界面获取焦点，按钮可以点击
     */
    @Override
    public void onPause() {
        super.onPause();
        //恢复默认状态
        if (mNfcAdapter != null)
            mNfcAdapter.disableForegroundDispatch(this);
    }
}
